﻿using MyShop;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 经典项目某某管理系统.Basic
{
    public partial class FrmSaleMan : Form
    {
        public FrmSaleMan()
        {
            InitializeComponent();
        }
        private void BindCmb()
        {
            this.comboBox1.Items.Add("请选择");
            this.comboBox1.Items.Add("男");
            this.comboBox1.Items.Add("女");
            this.comboBox1.SelectedIndex = 0;
            this.comboBox2.Items.Add("请选择");
            this.comboBox2.Items.Add("收银员");
            this.comboBox2.Items.Add("导购员");
            this.comboBox2.Items.Add("店长");
            this.comboBox2.SelectedIndex = 0;
        }

        private void BindData()
        {
            string sql = "select * from SalesMan ";
            DBHelper.PrepareSql(sql);
            DataTable dt = new DataTable();
            dt = DBHelper.ExecQuery();
            this.dataGridView1.DataSource = dt;
        }

        private void FrmSaleMan_Load(object sender, EventArgs e)
        {
            BindCmb();
            BindData();
        }


        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            string Mobile = this.dataGridView1.SelectedRows[0].Cells[2].Value.ToString();
            string sql = "select * from SalesMan where Mobile = @Mobile";
            DBHelper.PrepareSql(sql);
            DBHelper.SetParameter("Mobile", Mobile);
            DataTable dt = DBHelper.ExecQuery();
            this.textBox1.Text = dt.Rows[0]["SalesManName"].ToString();
            this.textBox2.Text = dt.Rows[0]["Mobile"].ToString();
            this.textBox3.Text = dt.Rows[0]["Pwd"].ToString();
            this.textBox4.Text = dt.Rows[0]["Wage"].ToString();
            this.textBox5.Text = dt.Rows[0]["CommissionRate"].ToString();
            foreach (string item in this.comboBox1.Items)
            {
                if (item.ToString().Equals(dt.Rows[0]["Gender"].ToString()))
                {
                    this.comboBox1.Text = item.ToString();
                }
            }
            foreach (string item in this.comboBox2.Items)
            {
                if (item.ToString().Equals(dt.Rows[0]["Role"].ToString()))
                {
                    this.comboBox2.Text = item.ToString();
                }
            }
        }

        public static bool IsMobile(string mobile)
        {
            if (string.IsNullOrEmpty(mobile))
                return false;
            return Regex.IsMatch(mobile, @"^(1)[3-9]\d{9}$");
        }

        private void button1_Click(object sender, EventArgs e)
        {

            if (!IsMobile(this.textBox2.Text.ToString()))
            {
                MessageBox.Show("请输入正确手机号");
                return;
            }
            if (this.textBox1.Text.Equals("") || this.textBox2.Text.Equals("") ||
                this.textBox3.Text.Equals("") || this.textBox4.Text.Equals("") ||
                this.textBox5.Text.Equals(""))
            {
                MessageBox.Show("文本框不能为空");
                return;
            }
            if (this.comboBox1.SelectedIndex == 0 || this.comboBox2.SelectedIndex == 0)
            {
                MessageBox.Show("下拉框还没有选择");
                return;
            }
            string sql = "insert into SalesMan(SalesmanName,Mobile,Pwd,Gender,Wage,CommissionRate,Role) " +
                 "values(@SalesmanName,@Mobile,@Pwd,@Gender,@Wage,@CommissionRate,@Role)";

            DBHelper.PrepareSql(sql);
            DBHelper.SetParameter("SalesmanName", this.textBox1.Text);
            DBHelper.SetParameter("Mobile", this.textBox2.Text);
            DBHelper.SetParameter("Pwd", this.textBox3.Text);
            DBHelper.SetParameter("Gender", this.comboBox1.Text);
            DBHelper.SetParameter("Wage", this.textBox4.Text);
            DBHelper.SetParameter("CommissionRate", this.textBox5.Text);
            DBHelper.SetParameter("Role", this.comboBox2.Text);
            int count = DBHelper.ExecNonQuery();
            if (count > 0)
            {
                MessageBox.Show("添加成功");
                BindData();
            }
            else
            {
                MessageBox.Show("添加失败");
            }
        }

        private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            DialogResult dr = MessageBox.Show("确定要删除吗?", "提示框", MessageBoxButtons.YesNo);
            if (dr == DialogResult.Yes)
            {
                string Mobile = this.dataGridView1.SelectedRows[0].Cells[2].Value.ToString();
                string sql = "delete  from SalesMan where Mobile = @Mobile";
                DBHelper.PrepareSql(sql);
                DBHelper.SetParameter("Mobile", this.textBox2.Text);
                int count = DBHelper.ExecNonQuery();
                if (count > 0)
                {
                    MessageBox.Show("删除成功");
                    BindData();
                }
                else
                {
                    MessageBox.Show("删除失败");
                }
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {

            if (!IsMobile(this.textBox2.Text.ToString()))
            {
                MessageBox.Show("请输入正确手机号");
                return;
            }
            if (this.textBox1.Text.Equals("") || this.textBox2.Text.Equals("") ||
                this.textBox3.Text.Equals("") || this.textBox4.Text.Equals("") ||
                this.textBox5.Text.Equals(""))
            {
                MessageBox.Show("文本框不能为空");
                return;
            }
            if (this.comboBox1.SelectedIndex == 0 || this.comboBox2.SelectedIndex == 0)
            {
                MessageBox.Show("下拉框还没有选择");
                return;
            }
            string sql = "update SalesMan  set SalesmanName=@SalesmanName,Mobile=@Mobile," +
                "Pwd =@Pwd,Gender=@Gender,Wage=@Wage,CommissionRate=@CommissionRate,Role = @Role " +
                 "where Mobile =@Mobile";

            DBHelper.PrepareSql(sql);
            DBHelper.SetParameter("SalesmanName", this.textBox1.Text);
            DBHelper.SetParameter("Mobile", this.textBox2.Text);
            DBHelper.SetParameter("Pwd", this.textBox3.Text);
            DBHelper.SetParameter("Gender", this.comboBox1.Text);
            DBHelper.SetParameter("Wage", this.textBox4.Text);
            DBHelper.SetParameter("CommissionRate", this.textBox5.Text);
            DBHelper.SetParameter("Role", this.comboBox2.Text);
            int count = DBHelper.ExecNonQuery();
            if (count > 0)
            {
                MessageBox.Show("更新成功");
                BindData();
            }
            else
            {
                MessageBox.Show("更新失败");
            }
        }
    }
}
